package com.mj.myvirtualapp.hooktest;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Process;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.mj.myvirtualapp.R;

import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {

    public static String TAG = "MainActivity";
    private ServiceConnection connection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        HashMap hashMap;
//        ConcurrentHashMap;
//        LinkedList;
        Executors.newSingleThreadExecutor();
        Executors.newFixedThreadPool(10);
//        ArrayBlockingQueue;
        Executors.newCachedThreadPool();
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                Log.e(TAG, "uncaughtException: " + e.getMessage());
                e.printStackTrace();
                Process.killProcess(0);


            }
        });


        Log.e(TAG,"onCreate");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.e(TAG,"onNewIntent");
    }

    //    public void testArray(int num) {
//        int[] arr=new int[num];
//        Class<Void> voidClass = void.class;
//        Array.newInstance();
//
//    }
    public void testStartActivity(View view) {
        startActivity(new Intent(this, EmptyActivity.class));

    }


    public void bindService(View v) {
        Intent bind = new Intent(this, RemoteService.class);
        connection = new ServiceConnection() {
            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.e(TAG, "binder onServiceDisconnected" );
            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                IMyAidlInterface aidlInterface = IMyAidlInterface.Stub.asInterface(service);
                int sum = 0;
                try {
                    sum = aidlInterface.add(1, 2);


                } catch (RemoteException e) {
                    e.printStackTrace();
                }

                Log.e(TAG, "binder onServiceConnected " +sum+"  "+ Thread.currentThread().getName());
            }
        };
        bindService(bind, connection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (connection != null)
            unbindService(connection);
    }
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

        }
    };
    String message="myMessage";
    int index=0;
    public void sendMessage(View view) {
        Message msg = Message.obtain();
        msg.obj=message+(++index);
        Toast.makeText(this, ""+msg.obj, Toast.LENGTH_SHORT).show();
        handler.sendMessageDelayed(msg,3000);


    }

    public void LeakActivity(View view) {
        startActivity(new Intent(this,LeakActivity.class));
    }
}
